///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Scene/Culling/Plane.h>
#include <Math/Vec3f.h>

//-----------------------------------------------------------------------------

Plane::Plane( const Vec3f &v1, const Vec3f &v2, const Vec3f &v3)
{
	set3Points(v1,v2,v3);
}

//-----------------------------------------------------------------------------

Plane::Plane()
{
}

//-----------------------------------------------------------------------------

Plane::~Plane()
{
}

//-----------------------------------------------------------------------------

void Plane::set3Points( const Vec3f &v1, const Vec3f &v2, const Vec3f &v3)
{
	Vec3f aux1, aux2;

	aux1 = v1 - v2;
	aux2 = v3 - v2;

	normal = aux2 ^ aux1;

	normal.normalize();
	point = v2;
	d = -(normal * point);
}

//-----------------------------------------------------------------------------

void Plane::setNormalAndPoint( const Vec3f &normal, const Vec3f &point)
{
	this->normal = normal;
	this->normal.normalize();
	d = -(this->normal * point);
}

//-----------------------------------------------------------------------------

void Plane::setCoefficients(f32 a, f32 b, f32 c, f32 d)
{
	// set the normal vector
	normal.set(a,b,c);
	//compute the lenght of the vector
	f32 l = normal.length();
	// normalize the vector
	normal.set(a/l,b/l,c/l);
	// and divide d by th length as well
	this->d = d/l;
}

//-----------------------------------------------------------------------------

f32 Plane::distance(const Vec3f &p)
{
	return (d + normal * p);
}

#if	defined(DEBUG)
//-----------------------------------------------------------------------------

void Plane::print()
{
	DEBUG_OUT("Plane( %f %f %f %f )\n",normal.x,normal.y,normal.z,d);
}
#endif

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
